package com.spring.springtrans.controller;

import com.spring.springtrans.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@Slf4j
@RequestMapping("/trans")
@RestController
public class TransactionalController {
    @Autowired
    private UserService userService;

    /**
     * 无注解,抛出异常,数据插入成功
     * @param name
     * @param password
     * @return
     */
//    @Transactional
    @RequestMapping("/r1")
    public String registry(String name,String password){

        Integer result = userService.registryUser(name,password);
        if(result==1){
            log.info("数据插入成功,影响行数:"+result);
        }
        int a = 10/0;
        return "注册成功";
    }
    @Transactional
    @RequestMapping("/r2")
    public String registry2(String name,String password){

        Integer result = userService.registryUser(name,password);
        if(result==1){
            log.info("数据插入成功,影响行数:"+result);
        }
        int a = 10/0;
        return "注册成功";
    }
    @Transactional
    @RequestMapping("/r3")
    public String registry3(String name,String password){

        Integer result = userService.registryUser(name,password);
        if(result==1){
            log.info("数据插入成功,影响行数:"+result);
        }
        try {
            int a = 10/0;

        }catch (Exception e){
            log.error("发生异常");
        }
        return "注册成功";
    }
    @Transactional
    @RequestMapping("/r4")
    public String registry4(String name,String password){

        Integer result = userService.registryUser(name,password);
        if(result==1){
            log.info("数据插入成功,影响行数:"+result);
        }
        try {
            int a = 10/0;

        }catch (Exception e){
            log.error("发生异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return "注册成功";
    }

    @Transactional(rollbackFor = Exception.class)
    @RequestMapping("/r5")
    public String registry5(String name,String password) throws IOException {

        Integer result = userService.registryUser(name,password);
        if(result==1){
            log.info("数据插入成功,影响行数:"+result);
        }
        if(true){
            throw new IOException();
        }
        return "注册成功";
    }
    @Transactional(rollbackFor = NullPointerException.class)
    @RequestMapping("/r6")
    public String registry6(String name,String password) {

        Integer result = userService.registryUser(name,password);
        if(result==1){
            log.info("数据插入成功,影响行数:"+result);
        }
        if(true){
            throw new RuntimeException();
        }
        return "注册成功";
    }
    @Transactional(isolation = Isolation.DEFAULT,propagation = Propagation.MANDATORY)
    @RequestMapping("/r7")
    public String registry7(String name,String password) {

        Integer result = userService.registryUser(name,password);
        if(result==1){
            log.info("数据插入成功,影响行数:"+result);
        }
        if(true){
            throw new RuntimeException();
        }
        return "注册成功";
    }
}
